package com.zw.mes.module.prodtaskmanager.service;

import com.zw.mes.module.overview.model.ProductModel;
import com.zw.mes.module.prodtaskmanager.entity.VwPreEnquiryFile;
import com.zw.mes.module.prodtaskmanager.mapper.ProdOrdMapper;
import com.zw.mes.module.prodtaskmanager.model.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 项目变更记录查询
 *
 * @author 徐一贺
 */
@Service
public class ProjectChangeQueryService {

    @Autowired
    private ProdOrdMapper prodOrdMapper;

    /**
     * 变更列表
     *
     * @param model
     * @return
     */
    public List<ProjectSchedulingModel> selectProjectChangeList(ProjectSchedulingModel model) {
        return prodOrdMapper.selectProjectChangeList(model);
    }

    /**
     * 查询变更
     *
     * @param proChangeId
     * @return
     */
    public ProjectModel selectChangeInfoById(Integer proChangeId) {
        return prodOrdMapper.selectChangeInfoById(proChangeId);
    }

    /**
     * 查询变更
     *
     * @param proChangeId
     * @return
     */
    public ProChange selectByPrimaryKey(Integer proChangeId) {
        return prodOrdMapper.selectByPrimaryKey(proChangeId);
    }

    /**
     * 查询意向
     *
     * @param preEnqId
     * @return
     */
    public PreEnquiry selectPreEnqById(Integer preEnqId) {
        return prodOrdMapper.selectPreEnqById(preEnqId);
    }

    /**
     * 查询附件
     *
     * @param id
     * @param origId
     * @return
     */
    public List<PreEnquiryFile> findChangeFileListByEnquiryId(Integer id, Integer origId) {
        return prodOrdMapper.findChangeFileListByEnquiryId(id, origId);
    }

    /**
     * @methodName: getVwPreEnquiryFileByPrimary
     * @description: 根据主键获取附件对象
     * @param: id
     * @return： com.zw.mes.module.prodtaskmanager.entity.VwPreEnquiryFile
     * @exception：
     * @author： 徐一贺 tel:18341916590
     * @designer： 陈欣，盖世奇
     * @leader： 陈欣 tel:18104194240
     * @date： 2021/4/7 9:49
     * @office: 智能控制设计科
     * @research institute： 智能控制设计所
     * @department： 机械设计院
     * @company: 辽宁忠旺机械设备制造有限公司
     * @group： 忠旺集团
     * @version: 1.0
     */
    public VwPreEnquiryFile getVwPreEnquiryFileByPrimary(Integer id) {
        return prodOrdMapper.getVwPreEnquiryFileByPrimary(id);
    }

    public List<ProductModel> selectProductList(ProductModel model) {
        String[] mouth = {"01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12"};
        List<ProductModel> productModelList = new ArrayList<>();
        Calendar cal = Calendar.getInstance(); //取得当前日历
        int month = cal.get(Calendar.MONTH);//取得月份
        int year = cal.get(Calendar.YEAR);
        for (int i = 0; i < 12; i++) {
            if ((month + 1 == i) && ("" + year).equals(model.getYear())) {
                break;
            }
            model.setTime(model.getYear() + "-" + mouth[i]);
            model.setType(1);
            double solderAllNum = prodOrdMapper.selectSolderAllNum(model);
            double solderCompleteNum = prodOrdMapper.selectSolderCompleteNum(model);
            double sprayAllNum = prodOrdMapper.selectSprayAllNum(model);
            double sprayCompleteNum = prodOrdMapper.selectSprayCompleteNum(model);
            int machiningAllNum = prodOrdMapper.selectMachiningAllNum(model);
            int machiningCompleteNum = prodOrdMapper.selectMachiningCompleteNum(model);
            ProductModel productModel = new ProductModel();
            productModel.setTime(model.getYear() + "-" + mouth[i]);
            productModel.setSolderAllNum(solderAllNum > 0 ? "" + solderAllNum : "0.00");
            productModel.setSolderCompleteNum(solderCompleteNum > 0 ? "" + solderCompleteNum : "0.00");
            productModel.setSolderSurplusNum( String.format("%.2f", (solderAllNum - solderCompleteNum)));
            productModel.setSprayAllNum(sprayAllNum > 0 ? "" + sprayAllNum : "0.00");
            productModel.setSprayCompleteNum(sprayCompleteNum > 0 ? "" + sprayCompleteNum : "0.00");
            productModel.setSpraySurplusNum(String.format("%.2f", (sprayAllNum - sprayCompleteNum)));
            productModel.setMachiningAllNum(getNum(machiningAllNum));
            productModel.setMachiningCompleteNum(getNum(machiningCompleteNum));

            productModel.setMachiningSurplusNum(String.format("%.2f",(Double.valueOf(getNum(machiningAllNum)) -Double.valueOf(getNum(machiningCompleteNum))))+"");
            productModelList.add(productModel);
        }

        return productModelList;
    }

    public String getNum(int num) {
        return num / 60 + "." + (num % 60 >= 10 ? num % 60 : "0" + num % 60);
    }

    public List<ProductModel> getProductModelList(String time) {
        Calendar cal = Calendar.getInstance(); //取得当前日历
        int month = cal.get(Calendar.MONTH)+1;//取得月份
        int year = cal.get(Calendar.YEAR);
        int date = cal.get(Calendar.DATE);
      String[] dateAndMouth= time.split("-");
      int dateNum = getDaysByYearMonth(Integer.valueOf(dateAndMouth[0]), Integer.valueOf(dateAndMouth[1]));
        List<ProductModel> productModelList = new ArrayList<>();
        for (int i = 1; i <= dateNum; i++) {
            ProductModel productModel = new ProductModel();
           double solderCompleteNum =  prodOrdMapper.selectSolderCompleteNumBydate(time+"-" + (i>9? i:("0"+i)));
            productModel.setSolderCompleteNum("铆焊完成重量："+ (solderCompleteNum > 0 ? "" + solderCompleteNum : "0.00"));
            double sprayCompleteNum = prodOrdMapper.selectSprayCompleteNumDate(time+"-" + (i>9? i:("0"+i)));
            productModel.setSprayCompleteNum("喷涂完成重量："+(sprayCompleteNum > 0 ? "" + sprayCompleteNum : "0.00"));
            int machiningCompleteNum = prodOrdMapper.selectMachiningCompleteNumDate(time+"-" + (i>9? i:("0"+i)));
            productModel.setMachiningCompleteNum("机加完成工时："+ getNum(machiningCompleteNum));
            productModel.setStaTime(time+"-" + (i>9? i:("0"+i)));
            if (i == dateNum){
                if (dateAndMouth[1].equals("12")){
                    productModel.setEnTime((Integer.valueOf(dateAndMouth[0]) +1)+"-01-01");
                }else {
                    productModel.setEnTime(dateAndMouth[0]+"-" + dateAndMouth[1]+"-01");
                }

            }else {
                productModel.setEnTime(time+"-" + ((i+1)>9? i:("0"+(i+1))));
            }
            if ( year==Integer.valueOf(dateAndMouth[0])&&month==Integer.valueOf(dateAndMouth[1]) &&i>date  ){
                productModel.setType(5);
            }else {
                productModel.setType(4);
            }
            productModelList.add(productModel);

        }
        return productModelList;
    }

    public static int getDaysByYearMonth(int year, int month) {
        Calendar a = Calendar.getInstance();
        a.set(Calendar.YEAR, year);
        a.set(Calendar.MONTH, month - 1);
        a.set(Calendar.DATE, 1);
        a.roll(Calendar.DATE, -1);
        int maxDate = a.get(Calendar.DATE);
        return maxDate;
    }

    public List<ProductModel> selectCL(ProductModel productModel) {
        List<ProductModel> productModelList = prodOrdMapper.getPruductDateNum(productModel);
        double solderCompleteNum =  prodOrdMapper.selectSolderCompleteNumBydate(productModel.getTime());
        double sprayCompleteNum = prodOrdMapper.selectSprayCompleteNumDate(productModel.getTime());
        int machiningCompleteNum = prodOrdMapper.selectMachiningCompleteNumDate(productModel.getTime());
        for (int i = 0; i < productModelList.size(); i++) {
            switch (productModelList.get(i).getRoute()){
                case "机加":
                    productModelList.get(i).setNumber(getNum(machiningCompleteNum));
                    break;
                case "铆焊":
                    productModelList.get(i).setNumber(""+solderCompleteNum);
                    break;
                case "喷涂":
                    productModelList.get(i).setNumber(""+sprayCompleteNum);
                    break;
            }
        }
        return  productModelList;
    }
}
